/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TTTWindow.java
 *
 * Created on 26.08.2009, 23:44:53
 */

package com.jtalker.client.plugins.TicTacToe;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

/**
 * 
 * @author christoph
 */
public class TTTWindow extends JFrame {

	public String symbols[] = { "", "X", "O" };

	// private static int ENEMY_REMOTE = 0;
	private static int ENEMY_AI = 1;
	private boolean userBegins = true;

	private int userSymbol = 1;
	private int enemyType = ENEMY_AI;
	private int aIQuality = 0;
	private String enemyName;

	private TTTGameLogic myGameLogic = null;
	private TicTacToe myPlugin;

	/** Creates new form TTTWindow */
	public TTTWindow(TicTacToe myPlugin) {
		this.myPlugin = myPlugin;
		initComponents();
	}

	public boolean setExternalAction(int index) {
		return this.myGameLogic.externalAction(index);
	}

	public void startNewGame(boolean userBegins, int userSymbol, int enemyType, int aIQuality, String enemyName) {
		System.out.println("----> in TTTWindow.startNewGame()");

		for (int i = 1; i < 10; i++) {
			try {
				((JButton) (this.getClass().getDeclaredField("jButton" + i).get(this))).setEnabled(true);
			} catch (Exception e) {
			}
		}

		this.userBegins = userBegins;
		this.userSymbol = userSymbol;
		this.enemyType = enemyType;
		this.aIQuality = aIQuality;
		this.enemyName = enemyName;

		if (this.myGameLogic == null){
			this.myGameLogic = new TTTGameLogic(this);
		}
		this.jLabelPlayerSelfInfo.setText(this.myPlugin.myName + ": " + this.myPlugin.winCounterUser);
		this.jLabelPlayerEnermyInfo.setText(this.myPlugin.enemyName + ": " + this.myPlugin.winCounterEnemy);

		this.myGameLogic.startGame(this.userBegins, this.userSymbol, this.enemyType, this.aIQuality);
	}

	public void gameEnds(int winner) {
		if (winner == 0) {
			// User wins
			this.myPlugin.winCounterUser++;
			this.markWinCombi(this.myGameLogic.lastWinCombi);
		} else if (winner == 1) {
			// enemy wins
			this.myPlugin.winCounterEnemy++;
			this.markWinCombi(this.myGameLogic.lastWinCombi);
		}

		this.jLabelPlayerSelfInfo.setText(this.myPlugin.myName + ": " + this.myPlugin.winCounterUser);
		this.jLabelPlayerEnermyInfo.setText(this.myPlugin.enemyName + ": " + this.myPlugin.winCounterEnemy);
	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
	 * content of this method is always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jPanelPlayer = new JPanel();
		jLabelPlayerSelfInfo = new JLabel();
		jLabelPlayerEnermyInfo = new JLabel();
		jPanelBottom = new JPanel();
		jButtonRestart = new JButton();
		jButtonCancel = new JButton();
		jPanelCenter = new JPanel();
		jButton1 = new JButton();
		jButton2 = new JButton();
		jButton3 = new JButton();
		jButton4 = new JButton();
		jButton5 = new JButton();
		jButton6 = new JButton();
		jButton7 = new JButton();
		jButton8 = new JButton();
		jButton9 = new JButton();
		jPanelStatus = new JPanel();
		jLabelStatus = new JLabel();

		setTitle("Tic-Tac-Toe Plugin");
		setMinimumSize(new Dimension(250, 250));

		jPanelPlayer.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
		jPanelPlayer.setLayout(new GridLayout(2, 1));
		jLabelPlayerSelfInfo.setText("Player1: 0");
		jPanelPlayer.add(jLabelPlayerSelfInfo);
		jLabelPlayerEnermyInfo.setText("Player2: 0");
		jPanelPlayer.add(jLabelPlayerEnermyInfo);
		getContentPane().add(jPanelPlayer, BorderLayout.EAST);

		jButtonRestart.setText("Restart");
		jButtonRestart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				restartGame(evt);
			}
		});
		jPanelBottom.add(jButtonRestart);
		jButtonCancel.setText("Cancel");
		jButtonCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelGame(evt);
			}
		});
		jPanelBottom.add(jButtonCancel);
		getContentPane().add(jPanelBottom, BorderLayout.SOUTH);

		jPanelCenter.setLayout(new GridLayout(3, 3));

		jButton1.setFont(new Font("Dialog", 1, 14));
		jButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});
		jPanelCenter.add(jButton1);

		jButton2.setFont(new Font("Dialog", 1, 14));
		jButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});
		jPanelCenter.add(jButton2);

		jButton3.setFont(new Font("Dialog", 1, 14));
		jButton3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton3ActionPerformed(evt);
			}
		});
		jPanelCenter.add(jButton3);

		jButton4.setFont(new Font("Dialog", 1, 14));
		jButton4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton4ActionPerformed(evt);
			}
		});
		jPanelCenter.add(jButton4);

		jButton5.setFont(new Font("Dialog", 1, 14));
		jButton5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton5ActionPerformed(evt);
			}
		});
		jPanelCenter.add(jButton5);

		jButton6.setFont(new Font("Dialog", 1, 14));
		jButton6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton6ActionPerformed(evt);
			}
		});
		jPanelCenter.add(jButton6);

		jButton7.setFont(new Font("Dialog", 1, 14));
		jButton7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton7ActionPerformed(evt);
			}
		});
		jPanelCenter.add(jButton7);

		jButton8.setFont(new Font("Dialog", 1, 14));
		jButton8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton8ActionPerformed(evt);
			}
		});
		jPanelCenter.add(jButton8);

		jButton9.setFont(new Font("Dialog", 1, 14));
		jButton9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				jButton9ActionPerformed(evt);
			}
		});
		jPanelCenter.add(jButton9);
		getContentPane().add(jPanelCenter, BorderLayout.CENTER);

		jPanelStatus.setLayout(new BorderLayout());
		jLabelStatus.setIcon(new ImageIcon(getClass().getResource("ttticon.png"))); // NOI18N
		jLabelStatus.setText("Waiting...");
		jPanelStatus.add(jLabelStatus, BorderLayout.CENTER);
		getContentPane().add(jPanelStatus, BorderLayout.NORTH);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jButton1ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton1ActionPerformed
		if (this.myGameLogic.userAction(1))
			this.myPlugin.setTurn(1);
	}// GEN-LAST:event_jButton1ActionPerformed

	private void cancelGame(ActionEvent evt) {// GEN-FIRST:event_jButtonCancelActionPerformed
		// Cancel clicked
		this.jLabelStatus.setText("Cancelling game...");
		this.myPlugin.endCurrentGame();
	}// GEN-LAST:event_jButtonCancelActionPerformed

	private void jButton2ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton2ActionPerformed
		if (this.myGameLogic.userAction(2))
			this.myPlugin.setTurn(2);
	}// GEN-LAST:event_jButton2ActionPerformed

	private void jButton3ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton3ActionPerformed
		if (this.myGameLogic.userAction(3))
			this.myPlugin.setTurn(3);
	}// GEN-LAST:event_jButton3ActionPerformed

	private void jButton4ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton4ActionPerformed
		if (this.myGameLogic.userAction(4))
			this.myPlugin.setTurn(4);
	}// GEN-LAST:event_jButton4ActionPerformed

	private void jButton5ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton5ActionPerformed
		if (this.myGameLogic.userAction(5))
			this.myPlugin.setTurn(5);
	}// GEN-LAST:event_jButton5ActionPerformed

	private void jButton6ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton6ActionPerformed
		if (this.myGameLogic.userAction(6))
			this.myPlugin.setTurn(6);
	}// GEN-LAST:event_jButton6ActionPerformed

	private void jButton7ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton7ActionPerformed
		if (this.myGameLogic.userAction(7))
			this.myPlugin.setTurn(7);
	}// GEN-LAST:event_jButton7ActionPerformed

	private void jButton8ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton8ActionPerformed
		if (this.myGameLogic.userAction(8))
			this.myPlugin.setTurn(8);
	}// GEN-LAST:event_jButton8ActionPerformed

	private void jButton9ActionPerformed(ActionEvent evt) {// GEN-FIRST:event_jButton9ActionPerformed
		if (this.myGameLogic.userAction(9))
			this.myPlugin.setTurn(9);
	}// GEN-LAST:event_jButton9ActionPerformed

	private void restartGame(ActionEvent evt) {// GEN-FIRST:event_jButtonRestartActionPerformed
		if (this.enemyType == ENEMY_AI) {
			this.restartGame();
		} else {
			this.myPlugin.startNewGame(true);
		}
	}// GEN-LAST:event_jButtonRestartActionPerformed

	public void setStatusString(String status) {
		this.jLabelStatus.setText(status.replaceAll("LOCAL", this.myPlugin.myName).replaceAll("ENEMY",
				this.myPlugin.enemyName));
	}

	public void setSymbolOnButton(int buttonIndex, int symbol) {
		try {
			((JButton) (this.getClass().getDeclaredField("jButton" + buttonIndex).get(this)))
					.setText(this.symbols[symbol]);
		} catch (Exception e) {
		}
	}

	public void markWinCombi(String combi) {
		int buttonNum = 0;
		for (int i = 0; i < 3; i++) {
			buttonNum = Integer.parseInt(combi.substring(i, i + 1));
			try {
				((JButton) (this.getClass().getDeclaredField("jButton" + buttonNum).get(this))).setEnabled(false);
			} catch (Exception e) {
			}
		}
	}

	public void restartGame() {
		for (int i = 1; i < 10; i++) {
			try {
				((JButton) (this.getClass().getDeclaredField("jButton" + i).get(this))).setEnabled(true);
			} catch (Exception e) {
			}
		}

		this.userBegins = !this.userBegins;
		this.myGameLogic.startGame(this.userBegins, this.userSymbol, this.enemyType, this.aIQuality);
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private JButton jButton1;
	private JButton jButton2;
	private JButton jButton3;
	private JButton jButton4;
	private JButton jButton5;
	private JButton jButton6;
	private JButton jButton7;
	private JButton jButton8;
	private JButton jButton9;
	private JButton jButtonCancel;
	private JButton jButtonRestart;
	private JLabel jLabelPlayerSelfInfo;
	private JLabel jLabelPlayerEnermyInfo;
	private JLabel jLabelStatus;
	private JPanel jPanelCenter;
	private JPanel jPanelStatus;
	private JPanel jPanelBottom;
	private JPanel jPanelPlayer;
	// End of variables declaration//GEN-END:variables

}
